package com.imooc.api.retry;

import com.imooc.exceptions.GraceException;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.utils.SMSUtilsRetry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;


import java.time.LocalDateTime;


@Component
@Slf4j
public class RetryComponent {

    @Retryable(
            value = {IllegalAccessException.class, ArrayIndexOutOfBoundsException.class}, // 指定重试的异常
            maxAttempts = 5,  // 最大重试次数
            backoff = @Backoff(delay = 1000, multiplier = 2) // 重试间隔1秒，倍数2 //   1/2/4/8/16
    )
    public boolean sendSmsWithRetry() {
        log.info("当前时间 Time={}", LocalDateTime.now());
        try {
            return SMSUtilsRetry.sendSMS();
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    // 兜底处理
    @Recover
    public boolean recover() {
        GraceException.display(ResponseStatusEnum.SYSTEM_OPERATION_ERROR);
        return false;
    }
}
